package com.likuicat.cataiagentbackend.exception;

import com.likuicat.cataiagentbackend.common.BaseResponse;
import com.likuicat.cataiagentbackend.common.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理类
 */
@RestControllerAdvice // 环绕切面类
@Slf4j
public class GlobalExceptionHandler {

    // 当全局捕获到BusinessException异常时，返回ResultUtils.error给前端
    /**
     * 自定义异常
     * @param e
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    public BaseResponse<?> handleException(BusinessException e) {
      log.error("BusinessException",e);
      return ResultUtils.error(e.getCode(),e.getMessage());
    }

    // 当全局捕获到RuntimeException异常时，返回ResultUtils.error给前端
    /**
     * 运行时异常
     * @param e
     * @return
     */
    @ExceptionHandler(RuntimeException.class)
    public BaseResponse<?> handleException(RuntimeException e) {
        log.error("RuntimeException",e);
        return ResultUtils.error(ErrorCode.SYSTEM_ERROR,"系统运行时异常");
    }

    //sa-token相关异常
   /* @ExceptionHandler(NotLoginException.class)
    public BaseResponse<?> notLoginException(NotLoginException e) {
        log.error("NotLoginException", e);
        return ResultUtils.error(ErrorCode.NOT_LOGIN_ERROR, e.getMessage());
    }

    @ExceptionHandler(NotPermissionException.class)
    public BaseResponse<?> notPermissionExceptionHandler(NotPermissionException e) {
        log.error("NotPermissionException", e);
        return ResultUtils.error(ErrorCode.NO_AUTH_ERROR, e.getMessage());
    }*/

}
